package com.aiit.controller;

import com.aiit.pojo.Customer;
import com.aiit.pojo.LingJian;
import com.aiit.pojo.User;
import com.aiit.pojo.order.SaleOrder;
import com.aiit.service.ICustomerService;
import com.aiit.service.ILingJianService;
import com.aiit.service.ISaleOrderService;
import com.aiit.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/main/sales")
public class SaleController {
    @Autowired
    ILingJianService lingJianService;
    @Autowired
    IUserService userService;
    @Autowired
    ISaleOrderService saleOrderService;
    @Autowired
    ICustomerService customerService;
    
    @RequestMapping("/omanagement")
    public String toOManagement(){
        return "/main/sales/omanagement";
    }
    @RequestMapping("/salesdelivery")
    public String toSalesDelivery(){
        return "/main/sales/salesdelivery";
    }
    @RequestMapping("/salesorder")
    public String toSalesOrder(){
        return "/main/sales/salesorder";
    }
    //查找相关数据
    @ResponseBody
    @RequestMapping("/login/salesdelivery")
    public Map<String,Object> loginSalesDelivery(){
        Map<String,Object> map = new HashMap<>();
        List<LingJian> lingJians = lingJianService.queryAll();
        List<User> users = userService.queryAll3();
        map.put("lingjians",lingJians);
        map.put("users",users);
        return map;
    }
    //创建订单，检查客户信息，并存入
    @ResponseBody
    @RequestMapping("/insert/salesdelivery")
    public Integer insertSalesDelivery(@RequestParam("lingjianName")String lingjianname,@RequestParam("number")Integer number,@RequestParam("cname")String cname,@RequestParam("phoneNumber")String phonenumber,@RequestParam("address")String address,@RequestParam("username")String username){
//        System.out.println(lingjianname+number+cname+phonenumber+address+username);
        if ((lingJianService.getOne(new QueryWrapper<LingJian>().eq("name", lingjianname)).getNumber()-number)<=0){
            return 2;
        }
        Integer i = customerService.queryByName(cname);
        Customer customer = new Customer();
        if (i!=1){
            customer.setName(cname);
            customer.setPhone(phonenumber);
            customerService.insert(customer);
        }
        SaleOrder saleOrder = new SaleOrder();
        LingJian lingJian = lingJianService.getOne(new QueryWrapper<LingJian>().eq("name",lingjianname));
        saleOrder.setLingjianId(lingJian.getId());
        saleOrder.setNumber(number);
        saleOrder.setTotalAmount(((double)number)* lingJian.getPrice());
        Customer customer1 = customerService.getOne(new QueryWrapper<Customer>().eq("name",cname));
        saleOrder.setCustomerId(customer1.getId());
        saleOrder.setState(1);
        saleOrder.setUserId(userService.getOne(new QueryWrapper<User>().eq("name",username)).getId());
        Integer result1 = saleOrderService.insert(saleOrder);
        Integer result2 = customerService.queryAddressByCId(customer1.getId(),address);
        if(result2!=1){
            Integer result3 = customerService.insertAddress(customer1.getId(),address);
            if (result3==1 && result1==1){
                LingJian lingJian1 = lingJianService.getOne(new QueryWrapper<LingJian>().eq("id", saleOrder.getLingjianId()));
                lingJian1.setNumber(lingJian1.getNumber()-saleOrder.getNumber());
                if (lingJianService.updateById(lingJian1)){
                    return 1;
                }else {
                    System.out.println("更新失败");
                    return  2;
                }
            }
        }
        return 2;
    }
    //已下单的订单的信息和查询条件的查询
    @ResponseBody
    @RequestMapping("/login/omanagement")
    public Map<String,Object> loginOManagement(){
        Map<String, Object> map = new HashMap<>();
        List<SaleOrder> saleOrders = saleOrderService.querySaleId();//获取下单状态的订单id
        map.put("saleOrders",saleOrders);
        List<LingJian> lingJians = lingJianService.queryAll();//获取所有零件信息
        map.put("lingjians",lingJians);
        List<Customer> customers = customerService.queryAll();
        map.put("customers",customers);
        System.out.println(map);
        return map;
    }
    //根据查询信息查询相应数据
    @ResponseBody
    @RequestMapping("/select/omanagement")
    public Map<String,Object> selectOManagement(@RequestParam("id")String id,@RequestParam("lingjianName")String lingjianName,@RequestParam("customerName")String customerName){
        Map<String, Object> map = new HashMap<>();
        if (!(id.equals("直接选择或搜索选择"))){//id选择了
            SaleOrder saleOrder= saleOrderService.queryById(Integer.parseInt(id));
            System.out.println(saleOrder);
            map.put("saleOrders",saleOrder);
            map.put("result",1);
        }else if (!lingjianName.equals("直接选择或搜索选择") && !customerName.equals("直接选择或搜索选择")){
            List<SaleOrder> saleOrders = saleOrderService.queryByLCId(lingJianService.getOne(new QueryWrapper<LingJian>().eq("name",lingjianName)).getId(),customerService.getOne(new QueryWrapper<Customer>().eq("name",customerName)).getId());
            System.out.println(saleOrders);
            map.put("saleOrders",saleOrders);
        }else if(!lingjianName.equals("直接选择或搜索选择")){
            List<SaleOrder> saleOrders = saleOrderService.queryByLId(lingJianService.getOne(new QueryWrapper<LingJian>().eq("name",lingjianName)).getId());
            System.out.println(saleOrders);
            map.put("saleOrders",saleOrders);
        }else if(!customerName.equals("直接选择或搜索选择")){
            List<SaleOrder> saleOrders = saleOrderService.queryByCId(customerService.getOne(new QueryWrapper<Customer>().eq("name",customerName)).getId());
            System.out.println(saleOrders);
            map.put("saleOrders",saleOrders);
        }else{
            List<SaleOrder> saleOrders = saleOrderService.queryAll1();
            System.out.println(saleOrders);
            map.put("saleOrders",saleOrders);
        }
        List<Customer> customers = customerService.queryAll();
        List<LingJian> lingJians = lingJianService.queryAll();
        List<User> users = userService.queryAll3();
        map.put("customers",customers);
        map.put("lingJians",lingJians);
        map.put("users",users);
        System.out.println(map);
        return map;
    }
    //退单操作，更新订单的状态
    @ResponseBody
    @RequestMapping("/update/omanagement")
    public Integer updateOManagement(SaleOrder saleOrder){
        SaleOrder saleOrder1 = saleOrderService.getById(saleOrder.getId());
        saleOrder1.setState(2);
        saleOrder1.setMsg(saleOrder.getMsg());
        if (saleOrderService.updateById(saleOrder1)){
            LingJian lingJian = lingJianService.getOne(new QueryWrapper<LingJian>().eq("id", saleOrder1.getLingjianId()));
            lingJian.setNumber(lingJian.getNumber()+saleOrder1.getNumber());
            if (lingJianService.updateById(lingJian)){
                return 1;
            }else {
                System.out.println("更新失败");
                return 2;
            }
        }else {
            return 2;
        }
    }
    //查询查询信息
    @ResponseBody
    @RequestMapping("/login/salesorder")
    public Map<String,Object> loginSaleOrder(){
        Map<String, Object> map = new HashMap<>();
        List<SaleOrder> saleOrders = saleOrderService.queryAll();
        List<LingJian> lingJians = lingJianService.queryAll();
        List<User> users = userService.queryAll3();
        map.put("saleOrders",saleOrders);
        map.put("lingJians",lingJians);
        map.put("users",users);
        return map;
    }
    //根据查询信息查询相应订单
    @ResponseBody
    @RequestMapping("/select/salesorder")
    public Map<String,Object> selectSalesOrder(@RequestParam("id")String id,@RequestParam("lingjianName")String lingjianName,@RequestParam("userId")String userId){
        Map<String, Object> map = new HashMap<>();
        if (!(id.equals("直接选择或搜索选择"))){//id选择了
            SaleOrder saleOrder= saleOrderService.queryById(Integer.parseInt(id));
            System.out.println(saleOrder);
            map.put("saleOrders",saleOrder);
            map.put("result",1);
        }else if (!lingjianName.equals("直接选择或搜索选择") && !userId.equals("直接选择或搜索选择")){
            List<SaleOrder> saleOrders = saleOrderService.queryByLUId(lingJianService.getOne(new QueryWrapper<LingJian>().eq("name",lingjianName)).getId(),Integer.parseInt(userId));
            System.out.println(saleOrders);
            map.put("saleOrders",saleOrders);
        }else if(!lingjianName.equals("直接选择或搜索选择")){
            List<SaleOrder> saleOrders = saleOrderService.queryByLId2(lingJianService.getOne(new QueryWrapper<LingJian>().eq("name",lingjianName)).getId());
            System.out.println(saleOrders);
            map.put("saleOrders",saleOrders);
        }else if(!userId.equals("直接选择或搜索选择")){
            List<SaleOrder> saleOrders = saleOrderService.queryByUId(Integer.parseInt(userId));
            System.out.println(saleOrders);
            map.put("saleOrders",saleOrders);
        }else{
            List<SaleOrder> saleOrders = saleOrderService.queryAll();
            System.out.println(saleOrders);
            map.put("saleOrders",saleOrders);
        }
        List<Customer> customers = customerService.queryAll();
        List<LingJian> lingJians = lingJianService.queryAll();
        List<User> users = userService.queryAll3();
        map.put("customers",customers);
        map.put("lingJians",lingJians);
        map.put("users",users);
        System.out.println(map);
        return map;
    }
    //删除操作删除相应订单
    @ResponseBody
    @RequestMapping("/delete/salesorder")
    public Integer deleteSalesOrder(Integer id){
        return saleOrderService.deleteById(id);
    }
}
